<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//物料
class Materials extends Common
{
	public function sale_huishou($page=1,$list=10){
		$where="m.m_type=2 and m.is_deleted=0 and m.is_actived=0 ";
		$sum=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $matarr=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->join("users uc","m.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","m.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("m.id")
    	->limit($start,$list)
        ->field("m.id,m.material_name,m.material_no,mt.type_name,m.unit,m.factory,ec.em_name as cname,m.created_on,em.em_name as mname,m.modifiled_on")
        ->select();

        for($i=0;$i<count($matarr);$i++){
        	$matarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["created_on"]));
        	if($matarr[$i]["modifiled_on"])
        		$matarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["modifiled_on"]));
        	
        }
        //查询类型列表
        $leilist=Db::table("pro_material_type")->where("is_actived=1 and is_deleted=0 and m_type=2")->select();
        $this->assign("leilist",$leilist);
        $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("matarr",$matarr);
		return $this->fetch('sale_huishou');
	}
	public function sale($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="m.m_type=2 and m.is_deleted=0 and m.is_actived=1 ";

    	if($data){
    		if(trim($data["material_no"])){
    			$where.="and m.material_no like '%{$data["material_no"]}%' ";
    		}
    		if(trim($data["material_name"])){
    			$where.="and m.material_name like '%{$data["material_name"]}%' ";
    		}
    		if(trim($data["material_type_id"])){
    			$where.="and m.material_type_id={$data["material_type_id"]}";
    		}
    	}else{
    		$data=["material_no"=>"","material_name"=>"","material_type_id"=>""];
    	}

    	$sum=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $matarr=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->join("users uc","m.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","m.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("m.id")
    	->limit($start,$list)
        ->field("m.id,m.material_name,m.material_no,mt.type_name,m.unit,m.factory,ec.em_name as cname,m.created_on,em.em_name as mname,m.modifiled_on")
        ->select();

        for($i=0;$i<count($matarr);$i++){
        	$matarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["created_on"]));
        	if($matarr[$i]["modifiled_on"])
        		$matarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["modifiled_on"]));
        	
        }
        //查询类型列表
        $leilist=Db::table("pro_material_type")->where("is_actived=1 and is_deleted=0 and m_type=2")->select();
        $this->assign("leilist",$leilist);
        $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("matarr",$matarr);
		return $this->fetch('sale');
	}
	public function produce_huishou($page=1,$list=10){
		$where="m.m_type=1 and m.is_deleted=0 and m.is_actived=0 ";
		$sum=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $matarr=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->join("users uc","m.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","m.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("m.id")
    	->limit($start,$list)
        ->field("m.id,m.material_name,m.material_no,mt.type_name,m.unit,m.factory,ec.em_name as cname,m.created_on,em.em_name as mname,m.modifiled_on")
        ->select();

        for($i=0;$i<count($matarr);$i++){
        	$matarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["created_on"]));
        	if($matarr[$i]["modifiled_on"])
        		$matarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["modifiled_on"]));
        	
        }
        //查询类型列表
        $leilist=Db::table("pro_material_type")->where("is_actived=1 and is_deleted=0 and m_type=1")->select();
        $this->assign("leilist",$leilist);
        $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("matarr",$matarr);
		return $this->fetch('produce_huishou');
	}
	public function produce($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="m.m_type=1 and m.is_deleted=0 and m.is_actived=1 ";

    	if($data){
    		if(trim($data["material_no"])){
    			$where.="and m.material_no like '%{$data["material_no"]}%' ";
    		}
    		if(trim($data["material_name"])){
    			$where.="and m.material_name like '%{$data["material_name"]}%' ";
    		}
    		if(trim($data["material_type_id"])){
    			$where.="and m.material_type_id={$data["material_type_id"]}";
    		}
    	}else{
    		$data=["material_no"=>"","material_name"=>"","material_type_id"=>""];
    	}

    	$sum=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $matarr=Db::table("pro_materials")->alias("m")
    	->join("pro_material_type mt","m.material_type_id=mt.id")
    	->join("users uc","m.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","m.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("m.id")
    	->limit($start,$list)
        ->field("m.id,m.material_name,m.material_no,mt.type_name,m.unit,m.factory,ec.em_name as cname,m.created_on,em.em_name as mname,m.modifiled_on")
        ->select();

        for($i=0;$i<count($matarr);$i++){
        	$matarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["created_on"]));
        	if($matarr[$i]["modifiled_on"])
        		$matarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($matarr[$i]["modifiled_on"]));
        	
        }
        //查询类型列表
        $leilist=Db::table("pro_material_type")->where("is_actived=1 and is_deleted=0 and m_type=1")->select();
        $this->assign("leilist",$leilist);
        $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("matarr",$matarr);
		return $this->fetch('produce');
	}
	//新增物料页面
	public function add_materias($tid,$mid=0){
		$type="生产";
		if($tid==2){
			$type="销售";
		}

		$typelist=Db::table("pro_material_type")->where(["is_actived"=>1,"is_deleted"=>0,"m_type"=>$tid])->select();
		//如是修改获取资料
		if($mid){
			$data=Db::table("pro_materials")->where(["id"=>$mid])
			->field("material_no,material_name,cost,material_type_id,unit,factory,weight,volume,format,remark")->find();
		}else{
			$data=["material_no"=>"","material_name"=>"","cost"=>"","material_type_id"=>0,"unit"=>"","factory"=>"","weight"=>"","volume"=>"","format"=>"","remark"=>""];
		}
		$this->assign("data",$data);
		$this->assign("typelist",$typelist);
		$this->assign("type",$type);
		$this->assign("tid",$tid);
		$this->assign("mid",$mid);
		return $this->fetch('add_materias');
	}
	//添加或修改
	public function add(){

		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        $mid=trim($datas["mid"]);
        $datam["m_type"]=trim($datas["tid"]);
        $datam["material_name"]=htmlspecialchars(trim($datas["material_name"]));
        $datam["material_no"]=htmlspecialchars(trim($datas["material_no"]));
        if(trim($datas["cost"]))
        	$datam["cost"]=trim($datas["cost"]);
        $datam["material_type_id"]=trim($datas["material_type_id"]);
        $datam["unit"]=htmlspecialchars(trim($datas["unit"]));
        $datam["factory"]=trim($datas["factory"]);
        if(trim($datas["weight"]))
        	$datam["weight"]=trim($datas["weight"]);
        if(trim($datas["volume"]))
        	$datam["volume"]=trim($datas["volume"]);
        $datam["format"]=htmlspecialchars(trim($datas["format"]));
        $datam["remark"]=htmlspecialchars(trim($datas["remark"]));

        if(!$datam["material_name"])
        	return json_encode(["msg"=>"物料名称不能为空","state"=>2]);
        if(!$datam["material_type_id"])
        	return json_encode(["msg"=>"物料类型必须选择","state"=>2]);
        if(!$datam["unit"])
        	return json_encode(["msg"=>"单位必须填写","state"=>2]);
        if(!$datam["factory"])
        	return json_encode(["msg"=>"出厂价格必须填写","state"=>2]);
        if($mid){
        	//修改
        	////判断重复
        	$ischongfu=Db::table("pro_materials")->where("is_deleted=0 and id<>{$mid} and (material_no='{$datam["material_no"]}' or material_name='{$datam["material_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"物料名或编号已存在","state"=>2]);
        	$datam["m_users_id"]=Session::get("login_id");
			$datam["modifiled_on"]=date("Y-m-d H:i:s");
			$datam["id"]=$mid;
			Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("pro_materials")->update($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"操作已成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }else{
        	//增加
        	if(!$datam["material_no"]){
        		//没有编号自动生成
        		if($datam["m_type"]==1){
        			$datam["material_no"]="MAP";
        		}else{
        			$datam["material_no"]="MAS";
        		}
        		$datam["material_no"]=$datam["material_no"].rand(0,100).time().Session::get("login_id");
        	}
        	//判断重复
        	$ischongfu=Db::table("pro_materials")->where("is_deleted=0 and (material_no='{$datam["material_no"]}' or material_name='{$datam["material_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"物料名或编号已存在","state"=>2]);
        	$datam["c_users_id"]=Session::get("login_id");
        	$datam["created_on"]=date("Y-m-d H:i:s");
        	Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("pro_materials")->insert($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"物料名称:{$datam["material_name"]}(编号:{$datam["material_no"]})添加成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }
	}

	//删除
	public function dropm(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["mid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_materials")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复
	public function huifum(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["mid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_materials")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}

    /**
     * 物料详情
     */
	public function view(){
	    $id = $this->request->param('id');
	    $data = db('pro_materials')->where('id',$id)->find();
	    $this->assign('data',$data);
	    return $this->fetch();
    }
}